package com.avidys.gwt.wom.client;

import net.ffxml.gwt.json.client.JsonRpc;

import com.avidys.common.gwt.net.ControlledWidget;
import com.avidys.common.gwt.net.Controller;
import com.avidys.common.gwt.net.Crypto;
import com.avidys.common.gwt.net.JsonHttp;
import com.avidys.common.gwt.net.Logging;
import com.avidys.common.gwt.net.JSONRPC.ServerResult;
import com.avidys.common.gwt.net.JSONRPC.ServerResult.extResult;
import com.extjs.gxt.ui.client.util.Params;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/*
 * A Grid to test any server communication
 * incorporates SID 
 */
public class TestServerPanel extends Composite implements ControlledWidget{

	String[] steps;
	TextBox[] params ;
	HTML[] results ;
	
	MyHandler handler = new MyHandler();
	PushButton[] toggleButtons;
	private Controller controller ;

	public TestServerPanel(String[] steps) {
		this.steps = steps;
		this.toggleButtons = new PushButton[steps.length];
		this.params = new TextBox[steps.length];
		this.results = new HTML[steps.length];
		
		VerticalPanel vp= new VerticalPanel();

		vp.add(new Label("Test Grid"));

		Grid grid = new Grid (steps.length,5);
		
		    // You can use the CellFormatter to affect the layout of the grid's cells.
		    CellFormatter cf = grid.getCellFormatter();
		    RowFormatter rowf = grid.getRowFormatter();
		    ColumnFormatter colf = grid.getColumnFormatter();
		    // .setWidth(0, 2, "256px");

			colf.setWidth(0,"10");
			colf.setWidth(1,"30");
			colf.setWidth(2,"200");
			colf.setWidth(3,"400");
			colf.setWidth(4,"35");

			for (int i = 0; i < steps.length; i++) {
				rowf.setVerticalAlign(i, HasVerticalAlignment.ALIGN_MIDDLE);
				grid.setText(i, 0, String.valueOf(i+1));
				grid.setText(i, 1, steps[i]);

				params[i] = new TextBox();
				params[i].setMaxLength(120);
				grid.setWidget(i, 2, params[i]);

				results[i] = new HTML("&nbsp;");
				results[i].setWidth("120");
				grid.setWidget(i, 3, results[i]);

				PushButton tb = new PushButton(" GO ","Done"); 
				tb.getElement().setId("tb-"+i);
				tb.setPixelSize(35, 15);
				tb.setTitle("Step "+i+"");
				tb.setTabIndex(i);
				tb.addClickHandler(handler);
				ToolTipConfig.setToolTip(tb, new ToolTipConfig("Step "+(i+1), "Press the buttons in a sequential order"));  
				grid.setWidget(i, 4, tb);
				toggleButtons[i]=tb;
			}
			grid.setBorderWidth(1);
			grid.setCellSpacing(0);
			grid.setCellPadding(4);
			vp.add(grid);
		
		initWidget(vp);
	}

	class MyHandler implements ClickHandler {
		int ID;
		
		public MyHandler(int iD) {
			super();
			ID = iD;
		}

		public MyHandler() { super();}

		public void onClick(ClickEvent event) {
			UIObject s= (UIObject) event.getSource(); 
			String id = s.getElement().getId();
			int idi = Integer.parseInt(id.substring(3));
			Logging.debug("ID "+ idi);
			sendServer(steps[idi],params[idi].getText(), id);
			results[idi].setText(Crypto.md5(params[idi].getText()));
		}
	}


	/* id is element id
	 * 
	 */
	public void sendServer(String method, String param, String id) {
		
		controller.sendTextToServer(method, param, id);
	}

	@Override
	public void onLogicError(String error) {
		Logging.info("Logic error");
	}

	@Override
	public void onRPCError(Throwable caught) {
		Logging.info("RPC error");
	}

	@Override
	public void setController(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void onTextReceived(ServerResult[] r) {
		if (r != null){
		Logging.info("Sequential Test Received ["+r[0].toString()+"]");
		String senderID = ((extResult) r[0].result).senderID;
		int idi = Integer.parseInt(senderID.substring(3));
		results[idi].setText(r[0].toString());
		}
	}

	public void testJR(){
	JsonRpc jsonRpc = new JsonRpc();

	AsyncCallback callback = new AsyncCallback() {
		public void onFailure(Throwable caught) {
			throw (new RuntimeException(caught));
		}

		public void onSuccess(Object result) {
		}
	};
	jsonRpc.request("/ws/listServers", null, null, "listServers", null, callback);
//	jsonRpc.request("/ws/", null, null, "updateUser", new Users[], callback);
	}

}
